<template>
	<view class="container bg-fff">
			<view class="item flex-space-between">
				<view class="item-label">姓名</view>
				<view class="item-value">
					<input type="text" v-model="form.name" placeholder="请输入姓名" />
				</view>
			</view>
			<view class="item flex-space-between">
				<view class="item-label">手机号</view>
				<view class="item-value">
					<input type="number" v-model="form.mobile" maxlength="11" placeholder="请输入手机" />
				</view>
			</view>
			<view class="item flex-space-between" style="margin-bottom: 100rpx;">
				<view class="item-label">推广区域</view>
				<view class="item-value">
					<input class="whitebg" style="width: 400rpx;" type="text" disabled v-model="form.region" @click="regionShow = !regionShow" />
					<u-icon name="arrow-right" color="#333333" size="28"></u-icon>
				</view>
			</view>
			<b-u-button-theme width="100%" height="90rpx" @click="send">提交</b-u-button-theme>
			
			<u-select v-model="regionShow" mode="mutil-column-auto" :default-value="regionDefault" :list="regionList" @confirm="regionConfirm"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					referee_id: '',
					region:"请选择省市区",
					province_id:'',
					city_id:'',
					county_id:'',
				},
				regionList: [],
				regionShow: false,
				regionDefault: [0, 0, 0],
			}
		},
		async onLoad() {
			await this.getRegion();
			this.getHttp();
		},
		methods: {
			getHttp() {
				let self = this;
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						uni.$u.api.tencentMapLocation(res.latitude, res.longitude).then(res => {
							if(res.data.result){
								this.form.province_name = res.data.result.address_component.province
								this.form.city_name =  res.data.result.address_component.city
								this.form.district_name = res.data.result.address_component.district
								
								//
								this.regionList.forEach((item, index) => {
									item.children.forEach((it, idx) => {
										it.children.forEach((i, ix) => {
											if (i.label == self.form.district_name) {
												console.log([item.value, it.value, i.value],'[index, idx, ix]')
												self.$set(self.form, 'province_id',item.value);
												self.$set(self.form, 'city_id',it.value);
												self.$set(self.form, 'county_id',i.value);
											}
										})
									})
								})
								
								self.$set(self.form, 'region',self.form.province_name + self.form.city_name + self.form.district_name);
								
							}
						})
					},
				});
			},
			async getRegion() {
				return new Promise((resolve, reject) => {
					uni.$u.api.getRegion().then(async(res)=> {
						this.regionList = res.regionData;
						resolve('ok');
					})
				})
			},
			regionConfirm(e) {
				this.form.province_id = e[0].value;
				this.form.city_id = e[1].value;
				this.form.county_id = e[2].value;
				this.$set(this.form, 'region', e[0].label + e[1].label + e[2].label);
				this.regionList.forEach((item, index) => {
					if (item.label == e[0].label) {
						item.children.forEach((it, idx) => {
							if (it.label == e[1].label) {
								it.children.forEach((i, ix) => {
									if (i.label == e[2].label) {
										this.regionDefault = [index, idx, ix];
									}
								})
							}
						})
					}
				})
			},
			send() {
				if(uni.$u.test.isEmpty(this.form.name)) {
					uni.showToast({
						title: "请输入姓名",
						icon: "none",
					})
					return;
				}
				if(uni.$u.test.isEmpty(this.form.mobile)) {
					uni.showToast({
						title: "请输入手机",
						icon: "none",
					})
					return;
				}
				if(uni.$u.test.isEmpty(this.form.province_id)) {
					uni.showToast({
						title: "请选择省市区",
						icon: "none"
					})
					return;
				}
				
				uni.$u.api.postAgentApply(this.form).then(res => {
					// if(res)
					// console.log(res);
					uni.showToast({
						title: "提交成功，请等待管理员审核",
						icon: "none",
					})
					setTimeout(function(){
						uni.navigateBack();
					},1000)
					// uni.showModal({
					// 	title: '提示',
					// 	content: '提交成功',
					// 	showCancel: false,
					// 	success: (res) => {
					// 		if (res.confirm) {
					// 			uni.navigateBack();
					// 			// console.log('用户点击确定');
					// 		} else if (res.cancel) {
					// 			// console.log('用户点击取消');
					// 		}
					// 	}
					// });
				})
			},
			
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx;
		
		.item {
			margin-bottom: 24rpx;
			display: flex;
			flex-direction: row;
			
			
			.item-label {
				font-size: 28rpx;
				color: #333333;
				width: 180rpx;
				
				text { 
					color: red;
				}
			}
			
			.item-value {
				flex-grow: 1;
				display: flex;
				align-items: center;
				justify-content: space-between;
				input {
					width: 438rpx;
					background: #F6F6F6;
					padding: 24rpx;
					border-radius: 16rpx;
				}
				.whitebg{
					background: #ffffff;
				}
			}
		}
		
		.qrcode {
			
			image {
				width: 500rpx;
				margin-bottom: 30rpx;
				box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(0, 0, 0, 0.09);
			}
		}
	}
</style>
